package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingVO;
import com.itheima.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController("backendOrderSettingController")
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;

    /**
     * 下载模板文件
     * @param filename
     */
    @GetMapping("/download")
    public void download(String filename, HttpServletResponse response){
        log.info("下载模板文件: {}", filename);
        orderSettingService.download(filename, response);
    }

    /**
     * 根据日期修改可预约人数
     * @param orderSetting
     * @return
     */
    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting){
        log.info("根据日期修改可预约人数:{}", orderSetting);
        orderSettingService.editNumberByOrderDate(orderSetting);
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }

    /**
     * 查询指定月份的预约数据
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month){
        log.info("查询:{}月的预约数据", month);
        List<OrderSettingVO> list = orderSettingService.getOrderSettingByMonth(month);
        return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
    }

    /**
     * 上传文件 批量导入预约设置数据
     * @param excelFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile){
        log.info("上传文件:{}", excelFile.getName());
        orderSettingService.upload(excelFile);
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }
}
